package portlet;

import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;

import oracle.adf.model.binding.DCIteratorBinding;
import oracle.adf.view.rich.component.rich.input.RichSelectOneChoice;

import oracle.jbo.Row;
import oracle.jbo.ViewCriteria;
import oracle.jbo.ViewCriteriaItem;
import oracle.jbo.ViewCriteriaRow;
import oracle.jbo.ViewObject;
import oracle.jbo.common.JboCompOper;

import oracle.portlet.bridge.adf.application.ADFBridgePortlet;

public class danhsachanpham extends ADFBridgePortlet {
    private Object soid;
    private Object chuyenmucid;
    private Object urlchitiet;
    private Object urldanhsach;
    private Object langid;
    private Object kieutapchiId;
    private Object imageid;
    private Object urlimage;
    private Object namxuatban;
    private String tenkieuanpham;
    private String sodientu;
    private String sogiay;
    private String soanpham;
    private String ngayxuatban;
    private String viewRender;
    //Labels
    private String labelAnpham;
    private String labelSodientu;
    private String labelSogiay;
    private String labelNgayxuatban;
    private String labelTimkiem;
    private String labelDanhsachAnpham;
    //Component
    private static String kieuSelectedValue;
    private SelectItem[] kieuElements = null;
    private static String namSelectedValue;
    private SelectItem[] namElements = null;
    private static String soSelectedValue;
    private SelectItem[] soElements = null;
    private RichSelectOneChoice kieuAnphamSelect = new RichSelectOneChoice();
    private RichSelectOneChoice namAnphamSelect = new RichSelectOneChoice();
    private RichSelectOneChoice soAnphamSelect = new RichSelectOneChoice();
    
    public danhsachanpham(){
//        DCIteratorBinding soMoiNhatIter =
//            Utils.getIteratorBinding("AptcSoMoiNhat1Iterator");    
//        soMoiNhatIter.getViewObject().getViewCriteriaManager().clearViewCriterias();
//        soMoiNhatIter.executeQuery();
//
//        DCIteratorBinding kieuIter =
//            Utils.getIteratorBinding("AptcKieuActiveView1Iterator");
//        kieuIter.getViewObject().getViewCriteriaManager().clearViewCriterias();
//        kieuIter.executeQuery();
    }

    public String getViewRender() {
        getLangid();
        getKieutapchiId();
        getSoid();
        getChuyenmucid();
        getNamxuatban();
        if (kieutapchiId == null && soid == null) {
            getSoMoiNhat();
        }
        getKieuAnPham();
        getSoNam();
        getSoTapChi();
        getSoHienTai();
        getChuyenmucThuocSo();
        return viewRender;
    }

    public void getSoMoiNhat() {
        DCIteratorBinding soMoiNhatIter =
            Utils.getIteratorBinding("AptcSoMoiNhat1Iterator");
        ViewObject vo = soMoiNhatIter.getViewObject();
        ViewCriteria vc = vo.createViewCriteria();
        vc.setName("SoMoiNhatCriteria");
        ViewCriteriaRow vcRow = vc.createViewCriteriaRow();
        ViewCriteriaItem rowViewCriteriaItem =
            vcRow.ensureCriteriaItem("NgonNgu");
        rowViewCriteriaItem.setOperator(JboCompOper.OPER_EQ);
        rowViewCriteriaItem.setValue(langid);
        vc.addRow(vcRow);
        vo.getViewCriteriaManager().clearViewCriterias();
        vo.applyViewCriteria(vc);
        vo.executeQuery();
        if (vo.getAllRowsInRange().length > 0) {
            vo.setCurrentRowAtRangeIndex(0);
            soid = vo.getRowAtRangeIndex(0).getAttribute("Id").toString();
            kieuSelectedValue =
                    vo.getRowAtRangeIndex(0).getAttribute("KieutapchiId").toString();
        }
    }

    public void getKieuAnPham() {
        DCIteratorBinding kieuActiveIter =
            Utils.getIteratorBinding("AptcKieuActiveView1Iterator");
        ViewObject vo = kieuActiveIter.getViewObject();
        ViewCriteria vc = vo.createViewCriteria();
        vc.setName("KieuViewCriteria");
        ViewCriteriaRow vcRow = vc.createViewCriteriaRow();
        ViewCriteriaItem rowViewCriteriaItem =
            vcRow.ensureCriteriaItem("NgonNgu");
        rowViewCriteriaItem.setOperator(JboCompOper.OPER_EQ);
        rowViewCriteriaItem.setValue(langid);
        vc.addRow(vcRow);
        vo.getViewCriteriaManager().removeApplyViewCriteriaName("KieuViewCriteria");
        vo.applyViewCriteria(vc);
        vo.executeQuery();
        if (vo.getAllRowsInRange().length > 0) {
            if (kieuSelectedValue != null) {
                if (kieutapchiId != null) {
                    kieuSelectedValue = kieutapchiId.toString();
                }
                kieuAnphamSelect.setValue(kieuSelectedValue);
            }
        }
    }

    public void getSoNam() {
        DCIteratorBinding kieuNamIter =
            Utils.getIteratorBinding("AptcSoNamView1Iterator");
        ViewObject vo = kieuNamIter.getViewObject();
        if (kieuSelectedValue != null) {
            ViewCriteria vc = vo.createViewCriteria();
            vc.setName("KieuNamViewCriteria");
            ViewCriteriaRow vcRow = vc.createViewCriteriaRow();
            ViewCriteriaItem rowViewCriteriaItem =
                vcRow.ensureCriteriaItem("KieutapchiId");
            rowViewCriteriaItem.setOperator(JboCompOper.OPER_EQ);
            rowViewCriteriaItem.setValue(kieuSelectedValue);
            vc.addRow(vcRow);
            vo.getViewCriteriaManager().clearViewCriterias();
            vo.applyViewCriteria(vc);
        }
        vo.executeQuery();
        if (vo.getAllRowsInRange().length > 0) {
            vo.setCurrentRowAtRangeIndex(0);
            namSelectedValue =
                    vo.getCurrentRow().getAttribute("Namxb").toString();
            if (namxuatban != null) {
                namSelectedValue = namxuatban.toString();
            }
            namAnphamSelect.setValue(namSelectedValue);
        }
    }

    public void getSoTapChi() {
        DCIteratorBinding soIterator =
            Utils.getIteratorBinding("AptcSoView1Iterator");
        ViewObject vo = soIterator.getViewObject();
        ViewCriteria vc = vo.createViewCriteria();
        vc.setName("SoViewCriteria");
        ViewCriteriaRow vcRow = vc.createViewCriteriaRow();
        ViewCriteriaItem rowViewCriteriaItem =
            vcRow.ensureCriteriaItem("NgonNgu");
        rowViewCriteriaItem.setOperator(JboCompOper.OPER_EQ);
        rowViewCriteriaItem.setValue(langid);
        if (kieuSelectedValue != null) {
            vcRow.setConjunction(ViewCriteriaRow.VC_CONJ_AND);
            ViewCriteriaItem rowViewCriteriaItem1 =
                vcRow.ensureCriteriaItem("KieutapchiId");
            rowViewCriteriaItem1.setOperator(JboCompOper.OPER_EQ);
            rowViewCriteriaItem1.setValue(kieuSelectedValue);
        }
        if (namSelectedValue != null) {
            vcRow.setConjunction(ViewCriteriaRow.VC_CONJ_AND);
            ViewCriteriaItem rowViewCriteriaItem2 =
                vcRow.ensureCriteriaItem("ToCharNamxuatbanYyyy");
            rowViewCriteriaItem2.setOperator(JboCompOper.OPER_EQ);
            rowViewCriteriaItem2.setValue(namSelectedValue);
        }
        vc.addRow(vcRow);
        vo.getViewCriteriaManager().clearViewCriterias();
        vo.applyViewCriteria(vc);
        vo.executeQuery();
        if (vo.getAllRowsInRange().length > 0) {
            vo.setCurrentRowAtRangeIndex(0);
            soSelectedValue = vo.getCurrentRow().getAttribute("Id").toString();
            if (soid != null) {
                soSelectedValue = soid.toString();
            }
            soAnphamSelect.setValue(soSelectedValue);
        }
    }

    public void getSoHienTai() {
        DCIteratorBinding soMoiNhatIter =
            Utils.getIteratorBinding("AptcSoMoiNhat1Iterator");
        ViewObject vo = soMoiNhatIter.getViewObject();
        ViewCriteria vc = vo.createViewCriteria();
        vc.setName("SoMoiViewCriteria");
        ViewCriteriaRow vcRow = vc.createViewCriteriaRow();
        ViewCriteriaItem rowViewCriteriaItem =
            vcRow.ensureCriteriaItem("NgonNgu");
        rowViewCriteriaItem.setOperator(JboCompOper.OPER_EQ);
        rowViewCriteriaItem.setValue(langid);
        if (kieuSelectedValue != null) {
            vcRow.setConjunction(ViewCriteriaRow.VC_CONJ_AND);
            ViewCriteriaItem rowViewCriteriaItem1 =
                vcRow.ensureCriteriaItem("KieutapchiId");
            rowViewCriteriaItem1.setOperator(JboCompOper.OPER_EQ);
            rowViewCriteriaItem1.setValue(kieuSelectedValue);
        }
        if (soSelectedValue != null) {
            vcRow.setConjunction(ViewCriteriaRow.VC_CONJ_AND);
            ViewCriteriaItem rowViewCriteriaItem2 =
                vcRow.ensureCriteriaItem("Id");
            rowViewCriteriaItem2.setOperator(JboCompOper.OPER_EQ);
            rowViewCriteriaItem2.setValue(soSelectedValue.toString());
        }
        vc.addRow(vcRow);
        vo.getViewCriteriaManager().removeApplyViewCriteriaName("SoMoiViewCriteria");
        vo.applyViewCriteria(vc);
        vo.setRangeSize(1);
        vo.executeQuery();
        if (vo.getAllRowsInRange().length > 0) {
            vo.setCurrentRowAtRangeIndex(0);
            if (soSelectedValue == null) {
                soSelectedValue =
                        vo.getCurrentRow().getAttribute("Id").toString();
            }
            tenkieuanpham =
                    vo.getCurrentRow().getAttribute("Kieutapchi").toString();
            sodientu = vo.getCurrentRow().getAttribute("Sodientu").toString();
            sogiay = vo.getCurrentRow().getAttribute("Sogiay").toString();
            soanpham = vo.getCurrentRow().getAttribute("Sotapchi").toString();
            imageid = vo.getCurrentRow().getAttribute("FileId");
            ngayxuatban =
                    vo.getCurrentRow().getAttribute("Ngayxuatban").toString();
        }
    }

    public void getChuyenmucThuocSo() {
        DCIteratorBinding soChuyenmucIter =
            Utils.getIteratorBinding("AptcSoChuyenmucView1Iterator");
        ViewObject vo = soChuyenmucIter.getViewObject();
        ViewCriteria vc = vo.createViewCriteria();
        vc.setName("SoChuyenmucViewCriteria");
        ViewCriteriaRow vcRow = vc.createViewCriteriaRow();
        if (soSelectedValue != null) {
            ViewCriteriaItem rowViewCriteriaItem =
                vcRow.ensureCriteriaItem("AptcSoId");
            rowViewCriteriaItem.setOperator(JboCompOper.OPER_EQ);
            rowViewCriteriaItem.setValue(soSelectedValue);
        }
        if (chuyenmucid != null) {
            vcRow.setConjunction(ViewCriteriaRow.VC_CONJ_AND);
            ViewCriteriaItem rowViewCriteriaItem2 =
                vcRow.ensureCriteriaItem("ChuyenmucId");
            rowViewCriteriaItem2.setOperator(JboCompOper.OPER_EQ);
            rowViewCriteriaItem2.setValue(chuyenmucid.toString());
        }
        vc.addRow(vcRow);
        vo.getViewCriteriaManager().removeApplyViewCriteriaName("SoChuyenmucViewCriteria");
        vo.applyViewCriteria(vc);
        vo.executeQuery();
    }

    public void timkiem() {
        getLangid();
        getUrldanhsach();
        Utils.redirect(urldanhsach + "?kieu=" + kieuSelectedValue + "&so=" +
                       soSelectedValue + "&namxb=" + namSelectedValue +
                       "&lang=" + langid);
    }

    public void kieuValueChanged(ValueChangeEvent valueChangeEvent) {
        kieuSelectedValue = valueChangeEvent.getNewValue().toString();
        getSoNam();
        getSoTapChi();
    }

    public void namValueChanged(ValueChangeEvent valueChangeEvent) {
        namSelectedValue = valueChangeEvent.getNewValue().toString();
        getSoTapChi();
    }

    public void soValueChanged(ValueChangeEvent valueChangeEvent) {
        soSelectedValue = valueChangeEvent.getNewValue().toString();
        DCIteratorBinding soHienTaiIter =
            Utils.getIteratorBinding("AptcSoMoiNhat1Iterator");
        soHienTaiIter.getViewObject().getViewCriteriaManager().clearViewCriterias();
        soHienTaiIter.executeQuery();
        if (soHienTaiIter.getAllRowsInRange().length > 0) {
            soHienTaiIter.setCurrentRowWithKeyValue(soSelectedValue);
        }
    }

    public Object getKieutapchiId() {
        try {
            kieutapchiId = Utils.getPortletRequest().getParameter("kieu");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return kieutapchiId;
    }

    public Object getSoid() {
        try {
            soid = Utils.getPortletRequest().getParameter("so");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return soid;
    }

    public Object getChuyenmucid() {
        try {
            chuyenmucid = Utils.getPortletRequest().getParameter("chuyenmuc");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return chuyenmucid;
    }

    public Object getUrldanhsach() {
        try {
            urldanhsach =
                    Utils.getPortletRequest().getParameter("urldanhsach");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return urldanhsach;
    }

    public Object getUrlchitiet() {
        try {
            urlchitiet = Utils.getPortletRequest().getParameter("urlchitiet");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return urlchitiet;
    }

    public Object getLangid() {
        langid = Utils.getPortalLocale("lang1").getLanguage();
        return langid;
    }

    public Object getTenkieuanpham() {
        return tenkieuanpham;
    }

    public String getSodientu() {
        return sodientu;
    }

    public String getSogiay() {
        return sogiay;
    }

    public String getSoanpham() {
        return soanpham;
    }

    public Object getImageid() {
        return imageid;
    }

    public Object getUrlimage() {
        try {
            urlimage = Utils.getPortletRequest().getParameter("urlimage");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return urlimage;
    }

    public Object getNamxuatban() {
        try {
            namxuatban = Utils.getPortletRequest().getParameter("namxb");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return namxuatban;
    }

    public String getNgayxuatban() {
        return ngayxuatban;
    }

    public String getLabelAnpham() {
        try {
            if (Utils.getPortletRequest().getParameter("tieudeportlet") !=
                null) {
                labelAnpham =
                        Utils.getPortletRequest().getParameter("tieudeportlet");
            } else {
                labelAnpham = Utils.getResource("TIEU_DE_AN_PHAM", "lang1");
            }
        } catch (Exception e) {
            labelAnpham = Utils.getResource("TIEU_DE_AN_PHAM", "lang1");
            e.printStackTrace();
        }
        return labelAnpham;
    }

    public String getLabelSodientu() {
        labelSodientu = Utils.getResource("SO_DIEN_TU", "lang1");
        return labelSodientu;
    }

    public String getLabelSogiay() {
        labelSogiay = Utils.getResource("SO_GIAY", "lang1");
        return labelSogiay;
    }

    public String getLabelNgayxuatban() {
        labelNgayxuatban = Utils.getResource("NGAY_XUAT_BAN", "lang1");
        return labelNgayxuatban;
    }

    public String getLabelTimkiem() {
        labelTimkiem = Utils.getResource("TIM_KIEM", "lang1");
        return labelTimkiem;
    }

    public String getLabelDanhsachAnpham() {
        labelDanhsachAnpham = Utils.getResource("DANH_SACH_AN_PHAM", "lang1");
        return labelDanhsachAnpham;
    }

    public String getLabelSo() {
        return Utils.getResource("SO", "lang1");
    }

    public void setKieuAnphamSelect(RichSelectOneChoice kieuAnphamSelect) {
        this.kieuAnphamSelect = kieuAnphamSelect;
    }

    public RichSelectOneChoice getKieuAnphamSelect() {
        return kieuAnphamSelect;
    }

    public void setNamAnphamSelect(RichSelectOneChoice namAnphamSelect) {
        this.namAnphamSelect = namAnphamSelect;
    }

    public RichSelectOneChoice getNamAnphamSelect() {
        return namAnphamSelect;
    }

    public void setSoAnphamSelect(RichSelectOneChoice soAnphamSelect) {
        this.soAnphamSelect = soAnphamSelect;
    }

    public RichSelectOneChoice getSoAnphamSelect() {
        return soAnphamSelect;
    }

    public void setKieuSelectedValue(String kieuSelectedValue) {
        this.kieuSelectedValue = kieuSelectedValue;
    }

    public String getKieuSelectedValue() {
        return kieuSelectedValue;
    }

    public void setKieuElements(SelectItem[] kieuElements) {
        this.kieuElements = kieuElements;
    }

    public SelectItem[] getKieuElements() {
        DCIteratorBinding kieuIterator =
            Utils.getIteratorBinding("AptcKieuActiveView1Iterator");
        int length = kieuIterator.getAllRowsInRange().length;
        kieuElements = new SelectItem[length];
        SelectItem item = null;
        for (int i = 0; i < length; i++) {
            Row row = kieuIterator.getRowAtRangeIndex(i);
            if (row != null) {
                item = new SelectItem();
                item.setValue(row.getAttribute("Id").toString());
                item.setLabel(row.getAttribute("Tieude").toString());
            }
            kieuElements[i] = item;
        }
        return kieuElements;
    }

    public void setNamSelectedValue(String namSelectedValue) {
        this.namSelectedValue = namSelectedValue;
    }

    public String getNamSelectedValue() {
        return namSelectedValue;
    }

    public void setNamElements(SelectItem[] namElements) {
        this.namElements = namElements;
    }

    public SelectItem[] getNamElements() {
        DCIteratorBinding namIterator =
            Utils.getIteratorBinding("AptcSoNamView1Iterator");
        int length = namIterator.getAllRowsInRange().length;
        namElements = new SelectItem[length];
        SelectItem item = null;
        for (int i = 0; i < length; i++) {
            Row row = namIterator.getRowAtRangeIndex(i);
            if (row != null) {
                item = new SelectItem();
                item.setValue(row.getAttribute("Namxb").toString());
                item.setLabel(row.getAttribute("Namxb").toString());
            }
            namElements[i] = item;
        }
        return namElements;
    }

    public void setSoSelectedValue(String soSelectedValue) {
        this.soSelectedValue = soSelectedValue;
    }

    public String getSoSelectedValue() {
        return soSelectedValue;
    }

    public void setSoElements(SelectItem[] soElements) {
        this.soElements = soElements;
    }

    public SelectItem[] getSoElements() {
        DCIteratorBinding soIterator =
            Utils.getIteratorBinding("AptcSoView1Iterator");
        int length = soIterator.getAllRowsInRange().length;
        soElements = new SelectItem[length];
        SelectItem item = null;
        for (int i = 0; i < length; i++) {
            Row row = soIterator.getRowAtRangeIndex(i);
            if (row != null) {
                item = new SelectItem();
                item.setValue(row.getAttribute("Id").toString());
                item.setLabel(getLabelSo() + " " +
                              row.getAttribute("Sotapchi").toString());
            }
            soElements[i] = item;
        }
        return soElements;
    }
}
